//
//  PlayerInfo.m
//  CandyCrushClone
//
//  Created by  Stefan Nestorov on 1/3/15.
//  Copyright (c) 2015 Nestorov Engineering. All rights reserved.
//

#import "GameInfo.h"
#import "Globals.h"
#import "LevelAdapter.h"
#import "HeroInfo.h"

@interface GameInfo () {
    
    
    
}

@end

@implementation GameInfo


- (instancetype)init
{
    self = [super init];
    if (self) {
        
        // Set the chosen hero type
        HeroInfo *chosenHeroInfo = [HeroInfo getChosenHeroInfo];
        
        _localPlayer = [[Player alloc] initWithHeroType:chosenHeroInfo.type];
        _otherPlayer = [[Player alloc] initWithHeroType:1];
        _isGameOver = NO;
        _winnerID = 0;
        _isAbortedGameByMe = NO;
        
    }
    return self;
}


+ (instancetype)sharedInfo
{
    
    static GameInfo *sharedInfo = nil;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        sharedInfo = [[self alloc] init];
    });
    
    return sharedInfo;
    
}

- (void)setIsMyTurn:(BOOL)isMyTurn
{
    
    NSLog(@"Set isMyTurn ? %@", isMyTurn == YES ? @"YES" : @"NO");
    
    _isMyTurn = isMyTurn;
    
}


- (void)setUserVariables:(NSArray *)userVariables forUser:(NSInteger)playerId
{
    
    // Determine for which player are the variables
    if ( _localPlayer.playerId == playerId ) {
        [self setVariables:userVariables forPlayer:_localPlayer];
    } else {
        [self setVariables:userVariables forPlayer:_otherPlayer];
    }
    
}


-(void)setVariables:(NSArray *)userVariables forPlayer:(Player *)player
{
    
    for (SFSUserVariable *variable in userVariables) {
        
        NSString *variableName = variable.name;
        
        if ( [variableName isEqualToString:@"name"] ) {
            player.name = [variable getStringValue];
        } else if ( [variableName isEqualToString:@"rank"] ) {
            player.rank = [variable getIntValue];
        } else if ( [variableName isEqualToString:@"wins"] ) {
            player.wins = [variable getIntValue];
        } else if ( [variableName isEqualToString:@"losses"] ) {
            player.losses = [variable getIntValue];
        } else if ( [variableName isEqualToString:@"money"] ) {
            player.money = [variable getIntValue];
        } else if ( [variableName isEqualToString:CHOSEN_HERO_TYPE] ) {
            player.hero.type = [variable getIntValue];
        } else if ( [variableName isEqualToString:CHOSEN_HERO_PRODUCES] ) {
            player.hero.produces = [variable getIntValue];
        } else if ( [variableName isEqualToString:CHOSEN_HERO_ATTACK] ) {
            player.hero.attack = [variable getIntValue];
        }
        
    }
    
}


- (void)setVariablesForBot:(NSDictionary *)botInfo
{
    
    _otherPlayer.name = botInfo[BOT_NAME];
    _otherPlayer.playerId = BOT_ID;
    _otherPlayer.wins = [botInfo[BOT_WINS] integerValue];
    _otherPlayer.losses = [botInfo[BOT_LOSSES] integerValue];
    _otherPlayer.rank = [botInfo[BOT_RANK] integerValue];
    _otherPlayer.hero.type = [botInfo[BOT_HERO_TYPE] integerValue];
    _otherPlayer.hero.produces = [botInfo[BOT_HERO_PRODUCES] integerValue];
    _otherPlayer.hero.attack = [botInfo[BOT_HERO_ATTACK] integerValue];
    
}


- (void)setFirstPlayerHeroConditions:(NSDictionary *)firstPlayerHeroConditions secondPlayerHeroConditions:(NSDictionary *)secondPlayerHeroConditions  firstPlayerHeroSkillsOnMyself:(NSDictionary *)firstPlayerHeroSkillsOnMyself firstPlayerHeroSkillsOnOtherPlayer:(NSDictionary *)firstPlayerHeroSkillsOnOtherPlayer secondPlayerHeroSkillsOnMyself:(NSDictionary *)secondPlayerHeroSkillsOnMyself secondPlayerHeroSkillsOnOtherPlayer:(NSDictionary *)secondPlayerHeroSkillsOnOtherPlayer withFirstPlayerId:(NSInteger)firstPlayerId
{
    
    // firstPlayer is LocalPlayer
    if ( _localPlayer.playerId == firstPlayerId ) {
        
        _localPlayer.hero.usageConditions = firstPlayerHeroConditions;
        _localPlayer.hero.skillsOnMyself = firstPlayerHeroSkillsOnMyself;
        _localPlayer.hero.skillsOnOtherPlayer = firstPlayerHeroSkillsOnOtherPlayer;
        
        _otherPlayer.hero.usageConditions = secondPlayerHeroConditions;
        _otherPlayer.hero.skillsOnMyself = secondPlayerHeroSkillsOnMyself;
        _otherPlayer.hero.skillsOnOtherPlayer = secondPlayerHeroSkillsOnOtherPlayer;
        
    } else {
        
        // secondPlayer is LocalPlayer
        _localPlayer.hero.usageConditions = secondPlayerHeroConditions;
        _localPlayer.hero.skillsOnMyself = secondPlayerHeroSkillsOnMyself;
        _localPlayer.hero.skillsOnOtherPlayer = secondPlayerHeroSkillsOnOtherPlayer;
        
        _otherPlayer.hero.usageConditions = firstPlayerHeroConditions;
        _otherPlayer.hero.skillsOnMyself = firstPlayerHeroSkillsOnMyself;
        _otherPlayer.hero.skillsOnOtherPlayer = firstPlayerHeroSkillsOnOtherPlayer;
        
    }
    
}


- (void)resetGameParameters
{
    
    _currentBoardCookiesSet = nil;
    [[LevelAdapter sharedAdapter] resetParameters];
    
}

@end
